/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.transformation;

import cz.mzk.oai.metadataRepository.harvest.DirectoryHarvester;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author Martin Rehanek, Martin.Rehanek@gmail.com
 */
public class MetadataTransformer {

    public static void transform(String fileFrom, String fileTo, Transformer transformer) throws IOException {
        try {
            File srcXml = new File(fileFrom);
            SAXReader reader = new SAXReader();
            Document srcDoc = reader.read(srcXml);
            transform(srcDoc, fileTo, transformer);
        } catch (DocumentException ex) {
            Logger.getLogger(DirectoryHarvester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void transform(Document srcDoc, String fileTo, Transformer transformer) throws IOException {
        try {
            DocumentResult result = new DocumentResult();
            transformer.transform(new DocumentSource(srcDoc), result);
            XMLWriter writer = new XMLWriter(new FileWriter(fileTo));
            writer.write(result.getDocument());
            writer.close();
        } catch (TransformerException ex) {
            Logger.getLogger(DirectoryHarvester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Only suitable for single transformation. For multiple transformations use transform(String fileFrom, String fileTo, Transformer transformer)
     * If you don't, the transformer object will be compiled multiple times and that is not efficient
     */
    public static void transform(String fileFrom, String fileTo, String fileTempl) throws IOException {
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer(new StreamSource(fileTempl));
            MetadataTransformer.transform(fileFrom, fileTo, transformer);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(MetadataTransformer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Only suitable for single transformation. For multiple transformations use transform(Document doc, String fileTo, Transformer transformer)
     * If you don't, the transformer object will be compiled multiple times and that is not efficient
     */
    public static void transform(Document doc, String fileTo, String fileTempl) throws IOException {
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer(new StreamSource(fileTempl));
            MetadataTransformer.transform(doc, fileTo, transformer);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(MetadataTransformer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
